<template>
  <basic-container>
    <avue-crud
      :option="option"
      :search.sync="search"
      :table-loading="loading"
      :data="data"
      :page.sync="page"
      :permission="permissionList"
      :before-open="beforeOpen"
      v-model="form"
      ref="crud"
      @row-update="rowUpdate"
      @row-save="rowSave"
      @row-del="rowDel"
      @search-change="searchChange"
      @search-reset="searchReset"
      @selection-change="selectionChange"
      @current-change="currentChange"
      @size-change="sizeChange"
      @refresh-change="refreshChange"
      @sort-change="sortChange"
      @on-load="onLoad"
    >
      <template slot-scope="{row}" slot="menu">
        <el-button
          v-if="permissionList.isOnBtn"
          size="small"
          type="text"
          @click.stop="handleSwitch(row)"
          >{{row.isOn === 1?'下架':'上架'}}</el-button
        >
      </template>
    </avue-crud>
  </basic-container>
</template>

<script>
import { apiDetail,apiList,apiIsOn,apiAdd,apiDel} from "@/api/rb-oem/shop/storeType";
import { mapGetters } from 'vuex'
export default {
  data() {
    return {
      form: {},
      query: {},
      search: {},
      loading: false,
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0
      },
      selectionList: [],
      option: {
        height: 'calc(50vh - 50px)',
        calcHeight: 30,
        tip: false,
        border: true,
        index: true,
        viewBtn: false,
        selection: false,
        // searchMenuSpan: 6,
        columnBtn: false,
        labelWidth: 120,
        searchLabelWidth: 120,
        column: [
          {
            label: "行业分类",
            prop: "firstCode",
            type: "select",
            span: 24,
            search: true,
            props: {
              label: 'firstName',
              value: 'firstCode',
            },
            cascaderItem: ['secondCode','thirdCode'],
            dicUrl:'/api/rb-oem/oem/mall/mall_class/list?current=-1&size=-1&type=1',
            dicFormatter:(data)=>{
              return data.data.records
            },
            rules: [{
              required: true,
              message: "请选择行业分类",
              trigger: "blur"
            }],
            hide: true,
          },
          {
            label: "行业分类",
            prop: "firstName",
            type: "input",
            //sortable: true,
            display: false,
          },
          {
            label: "行业大类",
            prop: "secondCode",
            type: "select",
            span: 24,
            search: true,
            props: {
              label: 'secondName',
              value: 'secondCode',
            },
            // cascader: ['thirdCode'],
            dicUrl:`/api/rb-oem/oem/mall/mall_class/list?current=-1&size=-1&type=2&firstCode={{key}}`,
            dicFormatter:(data)=>{
              return data.data.records
            },
            rules: [{
              required: true,
              message: "请选择行业大类",
              trigger: "blur"
            }],
            hide: true,
          },
          {
            label: "行业大类",
            prop: "secondName",
            type: "input",
            //sortable: true,
            span: 24,
            rules: [{
              required: true,
              message: "请输入行业类型名称",
              trigger: "blur"
            }],
            display: false,
          },
          {
            label: "行业小类",
            prop: "thirdCode",
            type: "select",
            span: 24,
            search: true,
            props: {
              label: 'thirdName',
              value: 'thirdCode',
            },
            dicUrl:`/api/rb-oem/oem/mall/mall_class/list?current=-1&size=-1&type=3&secondCode={{key}}`,
            dicFormatter:(data)=>{
              return data.data.records
            },
            rules: [{
              required: true,
              message: "请选择行业小类",
              trigger: "blur"
            }],
            hide: true,
          },
          {
            label: "行业小类",
            prop: "thirdName",
            type: "input",
            //sortable: true,
            span: 24,
            rules: [{
              required: true,
              message: "请输入行业类型名称",
              trigger: "blur"
            }],
            display: false,
          },
          {
            label: '品牌编号',
            prop: 'fourthCode',
            type: 'input',
            //sortable: true,
            display: false,
            hide: true
          },
          {
            label: '品牌',
            prop: 'fourthName',
            type: 'input',
            //sortable: true,
            span: 24,
            search: true,
            display: false,
          },
          {
            label: '品牌名称',
            prop: 'name',
            type: 'input',
            //sortable: true,
            span: 24,
            // search: true,
            rules: [
              {
                required: true,
                message: '请输入品牌名称',
                trigger: 'blur'
              }
            ],
            hide: true
          },
          {
            label: '优先级',
            prop: 'sortNo',
            min: 0,
            type: 'number',
            //sortable: true,
            span: 24,
            rules: [
              {
                required: true,
                message: '请输入优先级',
                trigger: 'blur'
              }
            ]
          },

          {
            label: '创建人',
            prop: 'createUserName',
            type: 'input',
            display: false
          },
          {
            label: '展示图',
            prop: 'logo',
            type: 'upload',
            //sortable: true,
            listType: 'picture-img',
            action: '/api/blade-resource/oss/endpoint/put-file-public',
            loadText: '图片上传中，请稍等',
            limit: 1,
            fileSize: 10240,
            span: 24,
            propsHttp: {
              res: 'data',
              url: 'link'
            },
            tip: '只能上传jpg/png文件，且不且不超过10Mb',
            rules: [
              {
                required: true,
                message: '请上传展示图',
                trigger: 'blur'
              }
            ]
          },
          {
            label: "创建时间",
            prop: "createTime",
            type: "input",
            overHidden: true,
            minWidth:140,
            //sortable: true,
            addDisplay: false,
            editDisplay: false,
            viewDisplay: false,
          },
          {
            label: '备注',
            prop: 'remark',
            type: 'textarea',
            //sortable: true,
            span: 24
          }
        ]
      },
      data: [{}]
    }
  },
  computed: {
    ...mapGetters(['permission']),
    permissionList() {
      return {
        addBtn: this.vaildData(this.permission.storeType_add, false),
        delBtn: this.vaildData(this.permission.storeType_del, false),
        editBtn: this.vaildData(this.permission.storeType_edit, false),
        isOnBtn: this.vaildData(this.permission.storeType_isOn, false)
      }
    },
    ids() {
      let ids = []
      this.selectionList.forEach((ele) => {
        ids.push(ele.id)
      })
      return ids.join(',')
    }
  },
  methods: {
    rowSave(row, done, loading) {
      let parm ={
        ...row,
        type:4
      }
      apiAdd(parm).then(
        () => {
          this.onLoad(this.page)
          this.$message({
            type: 'success',
            message: '操作成功!'
          })
          done()
        },
        (error) => {
          loading()
        }
      )
    },
    rowUpdate(row, index, done, loading) {
      let parm ={
        ...row,
        type:4
      }
      apiAdd(parm).then(
        () => {
          this.onLoad(this.page)
          this.$message({
            type: 'success',
            message: '操作成功!'
          })
          done()
        },
        (error) => {
          loading()
        }
      )
    },
    /**
     * @func 商品上架状态
     * @desc 商品上架 下架
     * @param {Object} row - 商品分类信息
     * */
     handleSwitch(row) {
      let param ={
        id:row.id,
        isOn:row.isOn===1?0:1
      }
      apiIsOn(param).then(() => {
        this.onLoad(this.page)
      })
    },
    rowDel(row) {
      this.$confirm('确定将选择数据删除?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          return apiDel(row.id)
        })
        .then(() => {
          this.onLoad(this.page)
          this.$message({
            type: 'success',
            message: '操作成功!'
          })
        })
    },
    
    handleDelete() {
      if (this.selectionList.length === 0) {
        this.$message.warning('请选择至少一条数据')
        return
      }
      this.$confirm('确定将选择数据删除?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          return apiDel(this.ids)
        })
        .then(() => {
          this.onLoad(this.page)
          this.$message({
            type: 'success',
            message: '操作成功!'
          })
          this.$refs.crud.toggleSelection()
        })
    },
    beforeOpen(done, type) {
      if (['edit', 'view'].includes(type)) {
        apiDetail({id:this.form.id}).then((res) => {
          this.form = res.data.data
        })
      }
      done()
    },
    searchReset() {
      this.query = {}
      this.onLoad(this.page)
    },
    searchChange(params, done) {
      this.query = params
      this.page.currentPage = 1
      this.onLoad(this.page, params)
      done()
    },
    selectionChange(list) {
      this.selectionList = list
    },
    selectionClear() {
      this.selectionList = []
      this.$refs.crud.toggleSelection()
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize
    },
    refreshChange() {
      this.onLoad(this.page, this.query)
    },
    onLoad(page, params = {}) {
      this.loading = true;
      params.type = 4;//1行业分类 2行业大类 3行业小类 4品牌分类

      apiList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
        this.loading = false;
        this.selectionClear();
      });
    },

    sortChange(val) {
      if (val.order == 'ascending') {
        //正序
        this.query.ascs = val.prop
        this.query.descs = ''
      } else {
        //倒序
        this.query.descs = val.prop
        this.query.ascs = ''
      }
      this.refreshChange()
    }
  }
}
</script>

<style>
</style>
